package yxy.game.pm2.active.module.openservicerank;

import org.joda.time.DateTime;
import org.redisson.api.RMap;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.game.pm2.bean.module.BaseGameRedisLockBean;

/**
 * <p>全服奖励领取记录<p/>
 * <p>给每个调这个接口的玩家初始化开服冲榜全服奖励领取信息<p/>
 *
 * @ClassName OpenServiceAllServiceModule
 * @Author jingtao-zheng
 * @Date 2022/7/11 17:46
 * @Version 1.0
 **/
public class OpenServiceAllServiceModule extends BaseGameRedisLockBean {
    private int taskid;//任务id
    private String playercode;//玩家code
    private DateTime firstcompletetime;//第一名完成时间
    private int type;//活动类型
    private int canget;//能否领取 1-可领取 2-已领取

    public int getTaskid() {
        return taskid;
    }

    public void setTaskid(int taskid) {
        this.taskid = taskid;
    }

    public String getPlayercode() {
        return playercode;
    }

    public void setPlayercode(String playercode) {
        this.playercode = playercode;
    }

    public DateTime getFirstcompletetime() {
        return firstcompletetime;
    }

    public void setFirstcompletetime(DateTime firstcompletetime) {
        this.firstcompletetime = firstcompletetime;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getCanget() {
        return canget;
    }

    public void setCanget(int canget) {
        this.canget = canget;
    }

    /**
     * 是否已经领取
     */
    public boolean isReward(){
        return getCanget() != 1;
    }


    //-------------------------redis

    public void set(){
        try {
            RMap<String, OpenServiceAllServiceModule> rmap = get(getPlayercode(),getType());
            rmap.put(String.valueOf(getTaskid()),this);
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    public void set(String realPlayerCode){
        try {
            RMap<String, OpenServiceAllServiceModule> rmap = get(realPlayerCode,getType());
            rmap.put(String.valueOf(getTaskid()),this);
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    static public RMap<String, OpenServiceAllServiceModule> get(String playerCode, int type) throws RedisException {
        RMap<String, OpenServiceAllServiceModule> map = getMapFromRedis(key(playerCode,type));
        return map;
    }

    static private String key(String playerCode, int type) {
        return String.format("PLAYER:%s:ALLSERINFO:%S", playerCode, type);
    }

    private String key() {
        return String.format("PLAYER:%s:ALLSERINFO:%S", getPlayercode(), getType());
    }

    static private RMap<String, OpenServiceAllServiceModule> getMapFromRedis(String key) throws RedisException {
        return RedisManager.client(redis()).getMap(key);
    }

}
